package com.winchain.chainbill.basic.invoice.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.basic.invoice.service.InvoiceDeferedService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillInvoiceDfrUploadSaveSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<? extends Object>> {
	@Autowired
	private InvoiceDeferedService invoiceDeferedService;
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private CbSndService cbSndService;

	public void onApplicationEvent(BillEvent<? extends Object> t) {
		Object source = t.getBill().getSource();
		if (source == null) {
			return;
		}
		CbDct dct = (CbDct) source;
		if (!dct.isUploadInvoiceDefer()) {
			return;
		}
		String dfrType = "1";
		String bizId = dct.getDctId();
		int cusDfrDays = dct.getCusDfrDays();
		CbRec rec = (CbRec) this.cbRecService.selectByKey(dct.getBillId());
		CbSnd snd = this.cbSndService.getBillSndById(rec.getRootId());
		String grpId = rec.getGrpId();
		String senderId = snd.getBuyerId();
		String applyDate = dct.getDctDate();
		int dctDays = dct.getDctDays().intValue();
		this.invoiceDeferedService.saveInvDfr(bizId, dfrType, cusDfrDays, grpId, senderId, applyDate, dctDays);
	}
}